package com.wlyuan.open.application.translator;

import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.core.marker.MappingStatus;
import com.wlyuan.open.domain.client.OpenClient;
import com.wlyuan.open.domain.client.utils.MappingOpenClient;
import com.wlyuan.open.vo.OpenClientVO;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * @author yuanzheng
 */
public class OpenClientTranslator {

    @Mapper(uses = {MappingIds.class, MappingStatus.class, MappingOpenClient.class})
    public interface Translator {

        @Mappings({
                @Mapping(source = "status", target = "statusName")
        })
        OpenClientVO translate(OpenClient source);

    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static OpenClientVO translate(OpenClient source) {
        var client = TRANSLATOR.translate(source);
        Optional.ofNullable(client).ifPresent(c -> {
            c.setHasBind(source.bind());
            c.setHasEnable(source.hasEnable());
        });
        return client;
    }

}
